﻿using K8.CBB;
using K8.LSF;
//using K8.LSF;
using KingerRobot.MexicoSailunMDC.Spi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace KingerRobot.SailunWebService
{
    [SoapService("MES2WMS")]
    [ServiceContract]
    public interface IMES2WMSService
    {

        /// <summary>
        /// 5.按条码出库（MES->WMS）由MES系统通过此接口对WMS进行要料
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [OperationContract]
        TirResult OutputTireByBarcode(List<OrderByBarcodeParam> orderByBarcodeParams);
        /// <summary>
        /// 6.按规格、数量出库（MES->WMS）由MES系统通过此接口对WMS进行要料
        /// </summary>
        /// <param name="orderByMaterialParams"></param>
        /// <returns></returns>
        [OperationContract]
        WebResult OutputTireByMaterial(List<OrderByMaterialParam> orderByMaterialParams);
    }

    public class ProductGreenTireParam
    {
        public string TireBarcode { get; set; }
        public string MaterialId { get; set; }
        public string ProductEquip { get; set; }
        public DateTime ProductTime { get; set; }
        public string ProductBatch { get; set; }
        public int TireLock { get; set; }
        public int QuailtyStatus { get; set; }
    }
    public class OrderByBarcodeParam
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNo { get; set; }
        /// <summary>
        /// 优先级
        /// </summary>
        public int OrderLevel { get; set; }
        /// <summary>
        /// 订单明细
        /// </summary>
        public List<OrderByBarcodeDetail> Details { get; set; }
    }
    public class OrderByBarcodeDetail
    {
        /// <summary>
        /// 订单明细Id
        /// </summary>
        public string OrderDetailId { get; set; }
        /// <summary>
        /// 胎胚条码
        /// </summary>
        public string TireBarcode { get; set; }
        /// <summary>
        /// 硫化机编号
        /// </summary>
        public string EquipNo { get; set; }
        /// <summary>
        /// 硫化机左右模
        /// </summary>
        public int EquipModel { get; set; }
    }
    public class OrderByMaterialParam
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNo { get; set; }
        /// <summary>
        /// 优先级
        /// </summary>
        public int OrderLevel { get; set; }
        /// <summary>
        /// 订单明细
        /// </summary>
        public List<OrderByMaterialDetail> Details { get; set; }
    }
    public class OrderByMaterialDetail
    {
        /// <summary>
        /// 订单明细Id
        /// </summary>
        public string OrderDetailId { get; set; }
        /// <summary>
        /// 物料id
        /// </summary>
        public string MaterialId { get; set; }
        /// <summary>
        /// 硫化机编号
        /// </summary>
        public string EquipNo { get; set; }
        /// <summary>
        /// 硫化机左右模
        /// </summary>
        public int EquipModel { get; set; }
        /// <summary>
        /// 请料数量
        /// </summary>
        public int OutputCount { get; set; }
    }

    public class WebResult
    {
        /// <summary>
        /// 影响行数
        /// </summary>
        public int OptionCount { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public int Status { get; set; }


    }
    public class TirResult
    {

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public int Status { get; set; }


    }

    public class MES2WMSServiceImpl : IMES2WMSService
    {
        public TirResult OutputTireByBarcode(List<OrderByBarcodeParam> orderByBarcodeParams)
        {
            // 存储文件
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"条码请料参数:{orderByBarcodeParams.ToJson()}" });
            List<RequestMaterialByBarcodeBean> requestMaterials = new();
            foreach (var orderByBarcodeParam in orderByBarcodeParams)
            {
                requestMaterials.Add(new()
                {
                    OrderNo = orderByBarcodeParam.OrderNo,
                    OrderLevel = orderByBarcodeParam.OrderLevel,
                    DetailBeans = orderByBarcodeParam.Details.Select(t => new RequestMaterialByBarcodeDetailBean
                    {
                        OrderDetailId = t.OrderDetailId,
                        TireBarcode = t.TireBarcode,
                        EquipNo = t.EquipNo,
                        EquipModel = t.EquipModel
                    }).ToList()
                });
            }
            var wcsService = K8.RSF.Client.Create<ICuringService>();
            var wr = wcsService.RequestMaterialByBarcode(requestMaterials);
            var result = new TirResult();
            result.Status = wr.status;
            result.Msg = wr.msg;
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"返回参数:{result.ToJson()}" });
            return result;
        }

        public WebResult OutputTireByMaterial(List<OrderByMaterialParam> orderByMaterialParams)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"规格数量请料参数:{orderByMaterialParams.ToJson()}" });
            var result = new WebResult();
            List<RequestMaterialByMaterialBean> requestMaterials = new();
            foreach (var orderByMaterialParam in orderByMaterialParams)
            {
                requestMaterials.Add(new()
                {
                    OrderNo = orderByMaterialParam.OrderNo,
                    OrderLevel = orderByMaterialParam.OrderLevel,
                    DetailBeans = orderByMaterialParam.Details.Select(t => new RequestMaterialByMaterialDetailBean
                    {
                        OrderDetailId = t.OrderDetailId,
                        MaterialId = t.MaterialId,
                        EquipNo = t.EquipNo,
                        EquipModel = t.EquipModel,
                        OutputCount = t.OutputCount
                    }).ToList()
                });
            }

            var wcsService = K8.RSF.Client.Create<ICuringService>();
            var wr = wcsService.RequestMaterialByMaterial(requestMaterials);
            result.Status = wr.status;
            result.Msg = wr.msg;
            result.OptionCount = wr.curingList.Count();
            return result;
        }


    }
}
